package com.lai.note.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.lai.note.R;
import com.lai.note.constant.Constant;
import com.lai.note.dto.ResponseResult;
import com.lai.note.interceptor.AddCookiesInterceptor;
import com.lai.note.interceptor.ReceivedCookiesInterceptor;
import com.lai.note.service.NoteService;
import com.lai.note.service.UserBaseService;
import com.lai.note.util.RetrofitRequest;

import okhttp3.Headers;
import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class WriteNoteFragment extends Fragment {
    public WriteNoteFragment(){
        super(R.layout.fragment_write_note);
    }

    private EditText et_note;
    private EditText et_title;
    private Button btn_note;

    //retrofit对象
    private Retrofit retrofit = null;
    private NoteService noteService = null;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        btn_note = getView().findViewById(R.id.btn_note);
        et_note = getView().findViewById(R.id.et_note);
        et_title = getView().findViewById(R.id.et_title);

        btn_note.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                retrofit = RetrofitRequest.getInstance(getContext());
                noteService = retrofit.create(NoteService.class);
                String title = et_title.getText().toString();
                String message = et_note.getText().toString();
                System.out.println("笔记内容为：-->"+message);
                Call<ResponseResult> call = noteService.addNewNote(title,message, "1");
                call.enqueue(new Callback<ResponseResult>() {
                    @Override
                    public void onResponse(Call<ResponseResult> call, Response<ResponseResult> response) {
                        ResponseResult res = response.body();
                        System.out.println("res:-->"+res);
                        if(res.getStatus()==200)
                            Toast.makeText(getActivity(),"保存成功！",Toast.LENGTH_SHORT).show();
                        else
                            Toast.makeText(getActivity(),"保存失败！\n好像遇到bug了^_^",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(Call<ResponseResult> call, Throwable t) {
                        t.printStackTrace();
                    }
                });
            }
        });

    }
}
